package com.bsg.upm.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.LoginService;

/**
 * 登录管理控制层
 * 
 * @author HCK
 *
 */
@Controller
@RequestMapping("/v1.0")
public class LoginController {

	@Resource
	private LoginService loginService;
	
	/**
	 * 登录验证
	 * 
	 * @param request
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public RespJson login(@RequestBody Map<String, Object> param, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = loginService.login(param);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	/**
	 * 登出
	 * 
	 * @param request
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	@ResponseBody
	public RespJson logout(HttpServletRequest request, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			loginService.logout(request);
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
}
